//
//  GDChoosePictureView.m
//  GuiderHealth
//
//  Created by mo on 2020/9/3.
//  Copyright © 2020 YM&EF. All rights reserved.
//

#import "GDChoosePictureView.h"
//#import "GDPhotoManager.h"
//#import "UIView+CreateItem.h"
//#import "UIView+MOLayout.h"
#import "BaseToolsKit.h"

#define KSCREEN_width [UIScreen mainScreen].bounds.size.width
#define KSCREEN_height [UIScreen mainScreen].bounds.size.height
@interface GDChoosePictureView()<UITableViewDelegate, UITableViewDataSource,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property(nonatomic, strong) id responder;

@property(nonatomic, strong) UITableView *tableView;

@end

@implementation GDChoosePictureView

- (instancetype)initWithFrame:(CGRect)frame andDelegate:(id<GDChoosePictureViewDelegate>)responderDelegate{
    if (self = [super initWithFrame:frame]) {
        
        self.responder = responderDelegate;
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    
    UIView *view = [[UIView alloc] initWithFrame:self.frame];
    view.backgroundColor =[[UIColor blackColor] colorWithAlphaComponent:0.0f];
    view.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cliclCancelButton)];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(cliclCancelButton)];
    [view addGestureRecognizer:tap];
    [view addGestureRecognizer:pan];
    [self addSubview:view];
    
    [self addSubview:self.tableView];
    
    [UIView animateWithDuration:0.25 animations:^{
        view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7f];

        self.tableView.mo_y = KSCREEN_height - 50 * 3 - SafeAreaTabbarDicatorHeight;

    }];
}


- (void)takePhoto{
    [[GDPhotoManager sharedManager] takePhotoWithBlock:^(UIImage * _Nonnull image) {
        [self finishImage:image];
    }];
}

- (void)choosePhoto{
    [[GDPhotoManager sharedManager] choosePhotoWithBlock:^(UIImage * _Nonnull image) {
        [self finishImage:image];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        //MARK:拍摄
        [self takePhoto];
    }else if (indexPath.row == 1) {
        //MARK:选择
        [self choosePhoto];
    }
    [self cliclCancelButton];
}

- (void)finishImage:(UIImage *)image{
    
    if ([self.responder respondsToSelector:@selector(choosePicture:)]) {
        [self.responder choosePicture:image];
    }
    
    if (self.choosePicture) {
        self.choosePicture(image);
    }
    
    [self cliclCancelButton];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    cell.selectionStyle = NO;
    
    NSArray *array = @[GDBaselocalString(@"拍摄"), GDBaselocalString(@"相册中选择"), GDBaselocalString(@"取消")];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, KSCREEN_width, 50)];
    label.text = array[indexPath.row];
    label.textColor = Color_51;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:12];
    [cell.contentView addSubview:label];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 49.5, KSCREEN_width - 30, 0.5)];
    lineView.backgroundColor = myLineColor;
    lineView.hidden = (indexPath.row == 2);
    [cell.contentView addSubview:lineView];
    
    return cell;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, KSCREEN_height, KSCREEN_width, 50 * 3 + SafeAreaTabbarDicatorHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = NO;
        _tableView.scrollEnabled = NO;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    }
    return _tableView;
}

- (void)cliclCancelButton{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.tableView.mo_y = KSCREEN_height;
    } completion:^(BOOL finished) {
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
        [self removeFromSuperview];
    }];
}

@end
